package com.eqdd.yiqidian.ui.mine.shenqing;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import com.eqdd.yiqidian.CreateNoticeActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.MultiTreeBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditRadioBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.select.SelectMultiBumenActivity;
import com.eqdd.yiqidian.utils.SingleChooseUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ActionSheetDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;

public class CreateNewsActivity extends SlimBaseMultiEditActivity {
    private CreateNoticeActivityCustom dataBinding;
    private ArrayList<MultiTreeBean> multiTreeBeanList;
    private ActionSheetDialog chooseDialogRelate;
    private String msg;
    private int bumenPosition;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_create_notice);
        initTopTitleBar(View.VISIBLE, "发布通知");
        bumenPosition = user.getIsAdmin() == 0 ? 1 : 2;
        initTopRightText("发布", v -> {
            if (checkParam()) {
                boolean left = false;
                if (user.getIsAdmin() != 0) {
                    left = ((SlimEditRadioBean) datas.get(1)).isLeft();
                }
                int primaryPosition = user.getIsAdmin() == 0 ? 2 : (left ? 2 : 3);
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.CREATE_NEWS)
                        .params("companyId", user.getCompanyId())
                        .params("userGuid", user.getGuid())
                        .params("newsName", datas.get(0).getContent())
                        .params("objectType", user.getIsAdmin() == 0 ? 1 : (left ? 0 : 1))
                        .params("objectDepartId", user.getIsAdmin() == 0 ? user.getDepartId() + "" : (left ? "0" : getDepartId()))
                        .params("newsTheme", datas.get(primaryPosition).getContent())
                        .params("newsContent", datas.get(primaryPosition + 1).getContent())
                        .params("createDepartId", user.getIsAdmin() == 0 ? user.getDepartId() : 0)
                        .params("duty", datas.get(primaryPosition + 2).getContent())
                        .params("newsCycle", datas.get(primaryPosition + 3).getContent())
                        .params("isAdmin", user.getIsAdmin())
                        .execute(new DialogCallBack<HttpResult>(this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    isSave = false;
                                    clearData();
                                    setResult(Config.SUCCESS);
                                    finish();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort(msg);
            }
        });
    }

    private String getDepartId() {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = multiTreeBeanList.size() - 1; i >= 0; i--) {
            stringBuilder.append(multiTreeBeanList.get(i).getId() + ";");
        }
        return stringBuilder.deleteCharAt(stringBuilder.lastIndexOf(";")).toString();
    }

    private boolean checkParam() {
        boolean left = false;
        if (user.getIsAdmin() != 0) {
            left = ((SlimEditRadioBean) datas.get(1)).isLeft();
        }
        if (TextUtils.isEmpty(datas.get(0).getContent())) {
            msg = "请输入通知名称";
            return false;
        }
        if ((user.getIsAdmin() != 0 && !left && TextUtils.isEmpty(datas.get(bumenPosition).getContent())) || (user.getIsAdmin() == 0 && TextUtils.isEmpty(datas.get(bumenPosition).getContent()))) {
            msg = "请选择部门";
            return false;
        }
        if (TextUtils.isEmpty(datas.get(left ? bumenPosition : bumenPosition + 1).getContent())) {
            msg = "请输入通知主题";
            return false;
        }
        if (TextUtils.isEmpty(datas.get(left ? bumenPosition + 1 : bumenPosition + 2).getContent())) {
            msg = "请选择通知内容";
            return false;
        }
        if (TextUtils.isEmpty(datas.get(left ? bumenPosition + 2 : bumenPosition + 3).getContent())) {
            msg = "请输入通知责任";
            return false;
        }
        if (TextUtils.isEmpty(datas.get(left ? bumenPosition + 3 : bumenPosition + 4).getContent())) {
            msg = "请选择通知周期";
            return false;
        }
        return true;
    }


    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (slimAdapterEx.getDataItem(p).getPosition()) {
            case 2:
                if (user.getIsAdmin() != 0) {
                    Bundle bundle = new Bundle();
                    bundle.putParcelableArrayList("list", multiTreeBeanList);
                    startActivityForResult(SelectMultiBumenActivity.class, bundle, Config.SELECT_MULTI_BUMEN);
                }
                break;
            case 6:
                if (chooseDialogRelate == null) {
                    SingleChooseUtil.getChooseDialog(CreateNewsActivity.this, 28, (sheet) -> {
                        chooseDialogRelate = sheet;
                        sheet.show();
                    }, (position, strings) -> {
                        slimAdapterEx.getDataItem(p).setContent(strings.get(position - 1));
                        slimAdapterEx.notifyItemChanged(p);
                    });
                } else {
                    chooseDialogRelate.show();
                }
                break;
        }
    }

    @Override
    protected void setEditBeans() {

        datas.add(new SlimEditEnterBean("通知名称", "", 0, "请输入通知名称"));
        if (user.getIsAdmin() != 0) {//人事
            datas.add(new SlimEditRadioBean("全体员工", "部门", true, 1));
        } else {//部门领导
            datas.add(new SlimEditHintBean("部门", user.getDepartment(), 2));
        }
        datas.add(new SlimEditEnterBean("主题", "", 3, "请输入通知主题"));
        datas.add(new SlimEditEnterBean("内容", "", 4, "请输入通知内容"));
        datas.add(new SlimEditEnterBean("责任", "", 5, "请输入通知责任"));
        datas.add(new SlimEditEnterBean("周期", "", 6, true, "请选择通知周期"));
        slimAdapterEx.register(R.layout.list_item_38, new SlimInjector<SlimEditRadioBean>() {
            @Override
            public void onInject(SlimEditRadioBean data, IViewInjector injector) {

                injector.text(R.id.rb_reject, data.getLeftContent())
                        .text(R.id.rb_agree, data.getRightContent())
                        .checked(data.isLeft() ? R.id.rb_reject : R.id.rb_agree, true)
                        .checked(data.isLeft() ? R.id.rb_agree : R.id.rb_reject, false)
                        .with(R.id.rb_reject, v -> {
                            ((RadioButton) v).setOnCheckedChangeListener((buttonView, isChecked) -> {
                                if (isChecked) {
                                    data.setLeft(true);
                                    injector.checked(R.id.rb_agree, !isChecked);
                                    if (slimAdapterEx.getDataItem(2).getPosition() == 2) {
                                        multiTreeBeanList = null;
                                        slimAdapterEx.getData().remove(2);
                                        slimAdapterEx.notifyItemRemoved(2);
                                    }
                                }
                            });
                        })
                        .with(R.id.rb_agree, v -> {
                            ((RadioButton) v).setOnCheckedChangeListener((buttonView, isChecked) -> {
                                if (isChecked) {
                                    data.setLeft(false);
                                    injector.checked(R.id.rb_reject, !isChecked);
                                    if (slimAdapterEx.getDataItem(2).getPosition() != 2 && user.getIsAdmin() != 0) {
                                        slimAdapterEx.getData().add(2, new SlimEditEnterBean("部门", "", 2, true, "请选择部门（多选）"));
                                        slimAdapterEx.notifyItemInserted(2);
                                    }
                                }
                            });
                        });
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.SUCCESS && requestCode == Config.SELECT_MULTI_BUMEN) {
            multiTreeBeanList = data.getParcelableArrayListExtra("list");
            slimAdapterEx.getDataItem(bumenPosition).setContent(multiTreeBeanList.size() + "个部门");
            slimAdapterEx.notifyItemChanged(bumenPosition);
        }
    }
}
